package cn.tfyhyc.springcloud.service.impl;

import cn.tfyhyc.springcloud.dao.OrderDao;
import cn.tfyhyc.springcloud.entity.Order;
import cn.tfyhyc.springcloud.service.AccountService;
import cn.tfyhyc.springcloud.service.OrderService;
import cn.tfyhyc.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author 头发又黑又长
 * @Date 2022/10/30 8:56
 * @email zwb15083976291@163.com
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {


    @Resource
    private OrderDao orderDao;

    @Resource
    private AccountService accountService;

    @Resource
    private StorageService storageService;


    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = {Exception.class}) // 定义全局事务
    public void create(Order order) {
        log.info("------------------> 开始新建订单");
        orderDao.create(order);


        log.info("-----------------> 订单微服务开始调用库存，做扣减 Count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("-----------------> 订单微服务开始调用库存，做扣减 END");

        log.info("-----------------> 订单微服务开始调用账户，做扣减 Money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("-----------------> 订单微服务开始调用账户，做扣减 END");
        // 修改订单状态
        log.info("-----------------> 修改订单状态");
        orderDao.update(order.getUserId(), 0);
        log.info("-----------------> 修改订单状态 END");


        log.info("下订单结束了~~~~O(∩_∩)O");
    }
}
